package com.geek.factory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geek.factory.entity.StockCheck;
import com.geek.factory.entity.Warehouse;
import com.geek.factory.mapper.StockCheckMapper;
import com.geek.factory.service.IStockCheckService;
import com.geek.factory.service.IWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-06-29
 */
@Service
public class StockCheckServiceImpl extends ServiceImpl<StockCheckMapper, StockCheck> implements IStockCheckService {
    @Autowired
    private IWarehouseService warehouseService;

    @Override
    public List<StockCheck> getPendingChecks(int limit) {
        // 查询状态为 draft 的待盘点任务
        List<StockCheck> checks = this.list(new QueryWrapper<StockCheck>()
                .eq("status", "draft")
                .orderByDesc("created_time")
                .last("LIMIT " + limit));

        // 关联仓库信息
        checks.forEach(check -> {
            if (check.getWarehouseId() != null) {
                Warehouse warehouse = warehouseService.getById(check.getWarehouseId());
                if (warehouse != null) {
                    check.setWarehouseName(warehouse.getWarehouseName());
                }
            }
        });

        return checks;
    }
}
